﻿using System;
using System.Collections.Generic;
using Authority.Logic;
using Authority.Model;
using Ioc.Library;
using Microsoft.AspNetCore.Mvc;
using Tool.Model;
using Web.Manage.App_Start;

namespace Web.Manage.Areas.Authority.Controllers
{
    /// <summary>
    /// 菜单管理控制器
    /// </summary>
    [Area("Authority")]
    public class MenusController : BaseController
    {
        #region 界面
        /// <summary>
        /// 列表页面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyView)]
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 增加界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 修改界面
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Modify()
        {
            return View();
        }

        /// <summary>
        ///选择按钮
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public IActionResult Buttons()
        {
            return View();
        }
        #endregion

        #region 查询操作

        /// <summary>
        /// 分页获取所有的可操作菜单
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult LoadListInfo()
        {
            var resultInfo = new ResultJsonInfo<List<MenusListResponse>>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();

                ISysMenuService menusService = UnityCIContainer.Instance.GetService<ISysMenuService>();
                resultInfo = menusService.LoadMenusList(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 获取菜单编码对应按钮信息[不加权限]
        /// </summary>
        /// <returns></returns>
        public ActionResult LoadButtons()
        {
            var resultInfo = new ResultJsonInfo<MenuButtonsListResponse>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<string>>();
                ISysMenuService menusService = UnityCIContainer.Instance.GetService<ISysMenuService>();
                resultInfo = menusService.LoadButtons(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

        #region 新增根节点
        /// <summary>
        /// 新增根节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.AddRoot)]
        public ActionResult AddRoot()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<MenusRootRequest>>();

                ISysMenuService menuService = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = menuService.AddRoot(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 新增子节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Add)]
        public ActionResult AddChild()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<MenusChildRequest>>();

                ISysMenuService menuService = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = menuService.AddChild(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 修改节点
        /// <summary>
        /// 修改节点信息
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Edit)]
        public ActionResult ModifyInfo()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<MenusModifyRequest>>();

                ISysMenuService menuService = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = menuService.Modify(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 移动顺序操作
        /// 上移（1：上移/2：下移）
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Top)]
        public ActionResult UpMove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据.
                var parametersInfo = DeserializeRequestData<ParametersInfo<MenusMoveRequest>>();

                ISysMenuService menuService = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = menuService.Move(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 移动顺序操作
        /// 下移（1：上移/2：下移）
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Bottom)]
        public ActionResult DownMove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据.
                var parametersInfo = DeserializeRequestData<ParametersInfo<MenusMoveRequest>>();

                ISysMenuService menuService = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = menuService.Move(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        /// <summary>
        /// 保存选中按钮
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyLoggedIn)]
        public ActionResult SaveButtons()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据.
                var parametersInfo = DeserializeRequestData<ParametersInfo<MenuButtonsSaveRequest>>();

                ISysMenuService menuService = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = menuService.SaveButtons(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }

        #endregion

        #region 删除节点
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <returns></returns>
        [UserAuthorization(true, FunctionValidateType.VerifyAction, ButtonAuthorizationCode.Remove)]
        public ActionResult Remove()
        {
            var resultInfo = new ResultJsonInfo<int>();
            try
            {
                //实体接收数据
                var parametersInfo = DeserializeRequestData<ParametersInfo<List<string>>>();

                ISysMenuService menuService = UnityCIContainer.Instance.GetService<ISysMenuService>();

                resultInfo = menuService.Remove(parametersInfo.parameters);
            }
            catch (Exception ex)
            {
                resultInfo.Success = false;
                resultInfo.Excep = ex;
            }
            return ReturnContent(resultInfo);
        }
        #endregion

    }
}